/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package PaqueteServelt;

import cl.tienda.modelo.Carrito;
import cl.tienda.modelo.Producto;
import cl.tienda.modelo.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 17125223-7
 */
public class ServletEAgrProducto3 extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletEAgrProducto3</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletEAgrProducto3 at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        
        HttpSession sesion = request.getSession();
        Vector<Carrito> carritoCompra= null;
        Vector<Producto> productos=(Vector<Producto>) 
                sesion.getAttribute("producto");
        
        Usuario usuario=(Usuario) sesion.getAttribute("usuario");
        if(sesion.getAttribute("carrito")!=null){
            carritoCompra=(Vector<Carrito>)  sesion.getAttribute("carrito");
        }else{
            carritoCompra=new Vector<Carrito>();
        }
        
        int indice=Integer.parseInt(request.getParameter("txtIndice").trim());
    
       int cantidad=Integer.parseInt(request.getParameter("txtCantidad").trim()); float subTotal=cantidad * productos.get(indice).getPrecio();
        //Creo el elemento carrito
        Carrito car=new Carrito();
        car.setCodigo(indice);
        car.setProducto(productos.get(indice));
        car.setUsuario(usuario);
        car.setCantidad(cantidad);
        car.setSubTotal(subTotal);
        //Agrego el carrito a mi vector
        carritoCompra.add(car);
        sesion.setAttribute("carrito",carritoCompra);
        PrintWriter out=response.getWriter();
        mostrarPagina(out, carritoCompra);
        out.close();
    
    
    
    
    }
    
    private void mostrarPagina(PrintWriter out,
                            Vector<Carrito> vcarrito){
         
          
           
           String color="";
          
                out.println("<table with=600><tr>");
                out.println("<td>Nombre</td>");
                out.println("<td>Stock</td>");
                out.println("<td>Precio</td>");
               
                out.println("</tr>");
            
            for (int i = 0; i < vcarrito.size(); i++) {
                color = "";
                if (i % 2 == 0) {
                    color = "#FFFFF";
                }
                vcarrito.get(i).getProducto();
                out.println("<tr color='" + color + "'>");
                out.println("<td>" + vcarrito.get(i).getProducto().getNombreproducto() + "</td>");
                out.println("<td>" + vcarrito.get(i).getProducto().getStock() + "</td>");
                out.println("<td>" + vcarrito.get(i).getProducto().getPrecio() + "</td>");
                 out.println("</tr>");
            }
            out.println("</table>");
         
        
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
